package com.fys.markbook.dialog

import com.fys.markbook.util.JDBCUtil
import com.intellij.notification.*
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.ui.MessageType
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.util.Version
import com.intellij.ui.EditorTextField
import java.awt.BorderLayout
import java.awt.Dimension
import java.awt.FlowLayout
import javax.swing.*

/**
 * @Title: AddNoteDialog
 * @Description: 右击 添加到笔记列表
 * @Author: fanyushuai
 * @Date: 2023/9/6 10:49
 * @Version V1.0
 */
class AddNoteDialog (etfMark: EditorTextField): DialogWrapper(true) {
    /**
     * 标题输入框
     */
    private var etfTitle: EditorTextField? = null

    /**
     * 内容输入框
     */
    private var etfMark: EditorTextField? = etfMark

    /**
     *  分类下拉框
     */
    private var eftCategory: JComboBox<String>? = null

    init {
        init()
        title = "添加到笔记"
    }

    /**
     * 弹出框标题
     */
    override fun createCenterPanel(): JComponent {
        val panel = JPanel(BorderLayout())
        etfTitle = EditorTextField("笔记标题")
        eftCategory = initCategory()

        //etfMark = EditorTextField(etfMark!!.getText())
        etfMark!!.preferredSize = Dimension(200, 100)
        etfTitle?.let { panel.add(it, BorderLayout.NORTH) }
        eftCategory?.let { panel.add(it, BorderLayout.CENTER) }
        etfMark?.let { panel.add(it, BorderLayout.SOUTH) }
        return panel
    }

    /**
     * 弹出框 按钮
     */
    override fun createSouthPanel(): JComponent {
        val panel = JPanel(FlowLayout())
        val btnAdd = JButton("添加到笔记列表")
        // 按钮点击事件处理
        btnAdd.addActionListener {
            // 获取标题
            val title = etfTitle!!.getText();
            // 获取内容
            val content = etfMark!!.getText();
            // 分类
            val category = eftCategory!!.selectedItem as String
            if("--请选择--".equals(category)){
                // 弹出一条错误提示
                Messages.showErrorDialog("请选择/键入分类", title);
                return@addActionListener
            }

            addNote(title,content, category);
        }
        panel.add(btnAdd)
        return panel
    }

    /**
     *  初始化 分类，数据库分组
     */
    private fun initCategory(): JComboBox<String>? {
        eftCategory = JComboBox()
        eftCategory!!.isEditable = true
        eftCategory!!.addItem("--请选择--")

        try {
            val connection = JDBCUtil.connection
            val sql = "SELECT DISTINCT(category) FROM note WHERE category IS NOT NULL;";
            val st = connection?.prepareStatement(sql);
            val rs = st?.executeQuery();
            while (rs!!.next()){
                eftCategory!!.addItem(rs.getString(1))
            }
        }catch (e:Exception){
            e.printStackTrace()
            println("初始化分组失败")
        }
        return eftCategory
    }

    /**
     *  点击 添加到笔记列表
     */
    private fun addNote(etfTitle: String, etfMark: String,etfCategory: String){

        // 添加通知

        val notificationGroup = NotificationGroupManager.getInstance().getNotificationGroup("notice") //NotificationGroup.create("notice", NotificationDisplayType.BALLOON,false, "点我", "点我",null);
        var message = "添加成功";
        try {
            val connection = JDBCUtil.connection


            val sql = "insert into note(`title`,`category`,`content`) values(?,?,?);";
            val st = connection?.prepareStatement(sql);
            st?.setString(1, etfTitle);
            st?.setString(2, etfCategory);
            st?.setString(3, etfMark);
            val rs = st?.executeUpdate();
            JDBCUtil.closeConnection(connection);

            this.close(1);

        }catch(e : Exception){
            e.printStackTrace()
            message = "添加失败"
        }

        val notification: Notification = notificationGroup.createNotification(message, if(message.equals("添加成功")) MessageType.INFO else MessageType.ERROR)
        Notifications.Bus.notify(notification)
    }
}
